package com.olclass.web.action;

import com.olclass.web.dao.DataBizMapper;
import com.olclass.web.domain.College;
import com.olclass.web.domain.User2lesson;
import com.olclass.web.service.CollegeService;
import com.olclass.web.service.User2lessonService;
import com.olclass.web.util.BaseObjectResponse;
import com.olclass.web.util.BaseResponse;
import com.olclass.web.util.CommonConstants;
import com.olclass.web.util.EasyUIResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * FileName: CollegeAction.java
 * Author:   Administrator
 * Date:     2021/3/23 19:18
 * Description:
 */
@RestController
@RequestMapping("/user2lesson")
public class User2lessonAction {

    private Logger logger = Logger.getLogger(User2lessonAction.class);

    @Autowired
    DataBizMapper dataBizMapper;

    @Autowired
    User2lessonService service;

    @RequestMapping(value = "/list.json", method = { RequestMethod.GET, RequestMethod.POST }, produces = {
            "application/json;charset=UTF-8" })
    @ResponseBody
    public BaseResponse queryByFilter(HttpServletRequest request, String filterParam, String sortParam, String page,
                                      String rows) {
        EasyUIResponse response = new EasyUIResponse();

        try {

            // 处理参数异常信息
            if (StringUtils.isBlank(page))
                throw new IllegalArgumentException("参数page不得为空");
            if (StringUtils.isBlank(rows))
                throw new IllegalArgumentException("参数rows不得为空");

            int start = Integer.valueOf(page) - 1;
            int limit = Integer.valueOf(rows);

            if (StringUtils.isBlank(filterParam)) {
                filterParam = "";
            }

            if (StringUtils.isBlank(sortParam)) {
                sortParam = "SORT_id_DESC";
            }

            Page<User2lesson> apks = service.queryByFilter(filterParam, sortParam, start, limit);
            response.setTotal(apks.getTotalElements());
            response.setRows(apks.getContent());

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            response.setStatus(CommonConstants.RESPONSE_STATUS_FAILURE);
            response.setMessage(ex.getMessage());
        }

        return response;
    }

    @RequestMapping(value = "/findById.json", method = { RequestMethod.GET, RequestMethod.POST }, produces = {
            "application/json;charset=UTF-8" })
    @ResponseBody
    public BaseResponse findById(HttpServletRequest request, String id) {
        BaseObjectResponse response = new BaseObjectResponse();

        try {

            User2lesson item = service.findById(id);
            response.setData(item);

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            response.setStatus(CommonConstants.RESPONSE_STATUS_FAILURE);
            response.setMessage(ex.getMessage());
        }
        return response;
    }
    @RequestMapping(value = "/save.json", method = { RequestMethod.GET, RequestMethod.POST }, produces = {
            "application/json;charset=UTF-8" })
    @ResponseBody
    public BaseResponse save(HttpServletRequest request,User2lesson item) {
        BaseResponse response = new BaseResponse();
        try {
            service.save(item);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            response.setStatus(CommonConstants.RESPONSE_STATUS_FAILURE);
            response.setMessage(ex.getMessage());
        }
        return response;
    }

    @RequestMapping(value = "/delete.json", method = { RequestMethod.GET, RequestMethod.POST }, produces = {
            "application/json;charset=UTF-8" })
    @ResponseBody
    public BaseResponse delete(HttpServletRequest request, String id) {
        BaseResponse response = new BaseResponse();
        try {
            service.delete(id.toString());
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            response.setStatus(CommonConstants.RESPONSE_STATUS_FAILURE);
            response.setMessage(ex.getMessage());
        }
        return response;
    }
}